// pages/resultype/diabeticretinopath/diabeticretinopath.js
const commonutil = require("../commonutil.js");
const screenresultservice = require("../../../apiservice/screen/screenresultservice.js");
const jsonData = require("../jsondata.js");
const app = getApp();
const regeneratorRuntime = app.globalData.regeneratorRuntime;
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		scid: '',
		customerid: '',
		leadName: '',
		addOrMod: 'add',
		//选项集
		preliminaryDiagnosisIndex: -1,
		result: [],
		haveOrNot: jsonData.haveOrNot,
    schoolId:'',
		/**糖尿病视网膜病变**/
		//结合病史，如满足以下条件之一则为糖网疑似患者
		medicalHistory: {
			new_scid: null,
			new_categoryid: 'LB0008',
			new_itemid: 'XM0031',
			new_contentid: 'NR0103',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//初步诊断
		preliminaryDiagnosis: {
			new_scid: null,
			new_categoryid: 'LB0008',
			new_itemid: 'XM0032',
			new_contentid: 'NR0106',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var options = options;
		if (options) {
			this.setData({
				leadName: options.leadname,
				customerid: options.sccustid,
				scid: options.scid,
				categoryid: options.categoryid
			})
			this.getDiabeticretinopathResults(options.scid, options.sccustid, options.categoryid);
      if(options.schoolId){
        this.setData({schoolId:options.schoolId});
      }
		}
	},
	//提交表单
	diabeticretinopathSubmit: function(e) {
		var createResult = this.getDiabeticretinopath();
		var that = this;
		commonutil.createScreenResults(createResult, function(res) {
			var idArray = res.result.split('|');
			for(var i = 0;i < idArray.length;i++){
				createResult[i].new_sc_resultId = idArray[i];
			}
			that.setData({
				addOrMod: "mod",
				medicalHistory:	createResult[0],
				preliminaryDiagnosis:	createResult[1],
			});
		});
	},
	//更新
	diabeticretinopathUpdate:function(){
		var updateResult = this.getDiabeticretinopath();
		commonutil.updateScreenResults(updateResult);
	},
	//查看报告
	enterDetail: async function(e) {
		var scid = this.data.scid;
		var leadid = this.data.customerid;
		var lead = {};
		var data = await commonutil.getLeadInfo(leadid);
		lead.leadid = data.leadId;
		lead.customername = data.lastName;
		lead.age = data.new_age;
		lead.sex = data.new_gender;
		lead.iphoneno1 = data.new_phone;
		lead.sfz = data.new_citizenid;
		lead.createdon = data.modifiedOn;
    var url = "/pages/print/letter/letter?scid=" + scid;
    if (this.data.schoolId)
      url += "&schoolId=" + this.data.schoolId;
		wx.navigateTo({
			url: url,
			events: {
				// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
				acceptDataFromOpenedPage: function(data) {
					console.log(data)
				},
				someEvent: function(data) {
					console.log(data)
				}
			},
			success: function(res) {
				// 通过eventChannel向被打开页面传送数据
				res.eventChannel.emit('acceptDataFromOpenerPage', {
					data: lead
				})
			}
		})
	},
	//获取结果集
	getDiabeticretinopathResults: async function(scid, customerid, categoryid) {
		wx.showLoading({
			title: '加载中...',
			mask: true,
		})
		var success = await screenresultservice.GetScreenResultByCategory(scid, customerid, categoryid);
		var reusltArray = success.data
		if (reusltArray.length == 0) {
			wx.hideLoading();
			return;
		}
		this.setData({
			addOrMod: 'mod'
		})
		for (let index in reusltArray) {
			var element = reusltArray[index];
			if (element.new_contentid === 'NR0103') {
				var optionsArray = element.new_result.split(',');
				this.setData({
					medicalHistory: element,
					result:optionsArray,
				})
			} else if (element.new_contentid === 'NR0106') {
				this.setData({
					preliminaryDiagnosis: element
				})			
			}
		}
		wx.hideLoading();
	},
	/**选项集、文本赋值**/
	onPreliminaryDiagnosisChange: function(e) {
		this.setData({
			preliminaryDiagnosisIndex: e.detail.index
		})
	},
	onMedicalHistoryChange: function(e) {
		this.setData({
			result: e.detail
		})
	},
	/**获取createResult**/
	getDiabeticretinopath: function() {
		var createResult = [];
		var scid = this.data.scid;
		var customerid = this.data.customerid;
		var medicalHistory = this.data.medicalHistory;
		var preliminaryDiagnosis = this.data.preliminaryDiagnosis;
		//周边前房
		var preliminaryDiagnosisIndex = this.data.preliminaryDiagnosisIndex;
		var haveOrNot = this.data.haveOrNot;
		if (preliminaryDiagnosisIndex > -1) {
			preliminaryDiagnosis.new_result = haveOrNot[preliminaryDiagnosisIndex];
		}
		//体征询问
		var selected = this.data.result;
		var str = '';
		if (selected != null && selected.length > 0) {
			str = selected.join(",");
		}
		medicalHistory.new_result = str;
		if(str!=''){
			createResult.push(medicalHistory);
		}
		createResult.push(preliminaryDiagnosis);
		//给各结果的筛查id/联系人id赋值
		createResult.forEach(element => {
			element.new_scid = scid;
			element.new_customerid = customerid;
		});
		return createResult;
	}

})
